package client.socket;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

public class DemoClientSocket {
	private Socket clientSocket;
	private int port;
	private String serverAddress;
	
	public DemoClientSocket(String serverAddress, int port){
		this.serverAddress = serverAddress;
		this.port = port;
	}
	
	public void connect(){
		if (isConnect()) {
			System.out.println("Ket noi den server thanh cong");
			DemoClientSend demoClientSend = new DemoClientSend(clientSocket);
			demoClientSend.start();
		}
	}
	
	public boolean disconnect(){
		try {
			clientSocket.close();
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	private boolean isConnect(){
		try {
			clientSocket = new Socket(serverAddress, port);
		} catch (UnknownHostException e) {
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
}
